package com.lagou.goods.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.common.pojo.Goods;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 分页查询商品
     * 查询条件：商品名称、分类、价格范围、库存范围、上架状态
     * SELECT * FROM goods
     * WHERE
     * `name` LIKE '%40%'
     * AND `type` = '手机'
     * AND price BETWEEN 4990 AND 5000
     * AND stock BETWEEN 1 AND 10
     * AND `status` = 1
     * LIMIT 0, 1;
     * @param name
     * @param type
     * @param priceS
     * @param priceE
     * @param stockS
     * @param stockE
     * @param status
     * @param offset
     * @param pageSize
     * @return
     */
    @Select("<script>" +
            "select * from goods " +
            "where 1=1 " +
            "<when test='name!=null'> and `name` LIKE CONCAT('%',#{name},'%') </when> " +
            "<when test='type!=null'> and `type` = #{type} </when> " +
            "and price BETWEEN #{priceS} AND #{priceE} " +
            "and stock BETWEEN #{stockS} AND #{stockE} " +
            "<when test='status!=null'> and status = #{status} </when> " +
            "LIMIT #{offset}, #{pageSize} " +
            "</script>")
    public List<Goods> getAllGoods(@Param("name") String name, @Param("type") String type,
                                   @Param("priceS") Double priceS, @Param("priceE") Double priceE,
                                   @Param("stockS") Integer stockS, @Param("stockE") Integer stockE,
                                   @Param("status") Integer status, @Param("offset") Integer offset,
                                   @Param("pageSize") Integer pageSize);

    /**
     * 查询商品信息总数
     * @return
     */
    @Select("<script>" +
            "select count(*) from goods " +
            "where 1=1 " +
            "<when test='name!=null'> and `name` LIKE CONCAT('%',#{name},'%') </when> " +
            "<when test='type!=null'> and `type` = #{type} </when> " +
            "and price BETWEEN #{priceS} AND #{priceE} " +
            "and stock BETWEEN #{stockS} AND #{stockE} " +
            "<when test='status!=null'> and status = #{status} </when> " +
            "</script>")
    public Integer getCountOfGoods(@Param("name") String name, @Param("type") String type,
                                   @Param("priceS") Double priceS, @Param("priceE") Double priceE,
                                   @Param("stockS") Integer stockS, @Param("stockE") Integer stockE,
                                   @Param("status") Integer status);

}
